// mmo_game_server/data/kafka_producer.go
package data

import (
    "fmt"
    "log"

    "github.com/segmentio/kafka-go"
)

// KafkaProducer 初始化Kafka生产者
func KafkaProducer() *kafka.Writer {
    return &kafka.Writer{
        Addr:     kafka.TCP("localhost:9092"),
        Topic:    "player_data",
       Balancer: &kafka.LeastBytes{},
    }
}

// ProduceMessage 生产消息到Kafka
func ProduceMessage(key, value []byte) error {
    w := KafkaProducer()
    defer w.Close()
    err := w.WriteMessages(kafka.Message{
        Key:   key,
        Value: value,
    })
    if err != nil {
        log.Printf("Failed to produce message to Kafka: %v", err)
        return err
    }
    return nil
}